#include "UdpServer.hpp"
#include "Route.hpp"
#include <memory>
#include "thread_pool.hpp"

typedef std::function<void()> task_t;

int main(int argc, char* argv[])
{
    if(argc != 2)
    {
        std::cerr << "ERROR:" << "Usage " << argv[0] << " port" << std::endl;
        exit(1);
    }

    //std::string ip = argv[1];
    uint16_t port = std::stoi(argv[1]);

    //创建路由服务
    Route r;

    //创建线程池
    auto pt = ThreadPool<task_t>::getInstance();

    //由线程池完成路由功能
    std::unique_ptr<UdpServer> p = std::make_unique<UdpServer>(port, [&pt, &r](int sockfd, const std::string& message, InetAddr& peer)
    {
        task_t task = std::bind(&Route::MessageRoute, &r, sockfd, message, peer);
        pt->Push(task);
    });

    p->Init();
    p->Start();

    // std::unique_ptr<UdpServer> p = std::make_unique<UdpServer>(port, [&r](int sockfd, const std::string& message, InetAddr& peer)
    // {
    //     r.MessageRoute(sockfd, message, peer);
    // });
    // p->Init();
    // p->Start();
    return 0;
}